package cn.edu.jxau.framework.dao.core;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.edu.jxau.framework.dao.entity.*;



/**
 * SQL解析器，解析，组装sql
 * @author Aaron
 * @param <T>
 *
 */
public class SqlParser {
	
	
	private  HashMap<String,View> viewMapper;

	SqlParser(){
		viewMapper = ParseXML.getInstance().parseViews();
	}
	
	String parseInsertSql(String viewName, final Map<String, Object> params,List<Object> param){
		
		View view = getView(viewName);
		HashMap<String, String> mapper = view.getMapper();

		StringBuffer buffer = new StringBuffer();
		buffer.append("insert into ");
		String tableName = view.getAddress();
		buffer.append(tableName+"(");

		for (String key : params.keySet()) {
			Object value = params.get(key);
			String column = mapper.get(key);
			if(column!=null){
				buffer.append(column+",");
				param.add(value);
			}
		}
		buffer.replace(buffer.length()-1, buffer.length(), ") value(");

		for(int i=0;i<param.size();i++){
			buffer.append("?,");
		}
		buffer.replace(buffer.length()-1, buffer.length(), ")");
		
		return buffer.toString();
	}
	
	String parseUpdateSql(String viewName, final Map<String, Object> params,List<Object> param){
		
		View view = getView(viewName);
		HashMap<String, String> mapper = view.getMapper();
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("update ");
		String tableName = view.getAddress();
		buffer.append(tableName+" set ");
		
	
		for (String key : params.keySet()) {
			Object value = params.get(key);
			String column = mapper.get(key);
			if(column!=null){
				buffer.append(column+"=?,");
				param.add(value);
			}

		}
		buffer.replace(buffer.length()-1, buffer.length(), " where ");
		
		HashMap<String, String> where = view.getMapperwhere();
		
		for (String key : params.keySet()) {
			Object value = params.get(key);
			String column = where.get(key);
			if(column!=null){

				String[] str = column.split(":");

				if(str.length==2){
					buffer.append(" "+str[0]+"=? "+str[1]);
				}else{
					buffer.append(" "+str[0]+"=? and");
				}
				
				param.add(value);
			}

		}
				
		buffer.replace(buffer.length()-3, buffer.length(), "");
		
		return buffer.toString();
	}
	
	String parseDeleteSql(String viewName, final Map<String, Object> params,List<Object> param){
		
		View view = getView(viewName);		
		StringBuffer buffer = new StringBuffer();
		buffer.append("delete from ");
		String tableName = view.getAddress();
		buffer.append(tableName+" where ");
		
	

		HashMap<String, String> where = view.getMapperwhere();
		
		for (String key : params.keySet()) {
			Object value = params.get(key);
			String column = where.get(key);
			if(column!=null){

				String[] str = column.split(":");

				if(str.length==2){
					buffer.append(" "+str[0]+"=? "+str[1]);
				}else{
					buffer.append(" "+str[0]+"=? and");
				}
				
				param.add(value);
			}
		
		}
				
		buffer.replace(buffer.length()-3, buffer.length(), "");
		
		return buffer.toString();
	}
	
	String parseSelectSql(String viewName, final Map<String, Object> params,List<Object> param){
		
		View view = getView(viewName);
		
		
		StringBuffer buffer = new StringBuffer();
		HashMap<String,String> map = view.getRelustName();
		if(map.size()==0){
			buffer.append("select * from ");
		}else{
			buffer.append("select ");
			for (String key : map.keySet()) {
				buffer.append(key+" as "+map.get(key)+",");
			}
			buffer.replace(buffer.length()-1, buffer.length(), " from ");
		}
		String tableName = view.getAddress();
		
		HashMap<String, String> where = view.getMapperwhere();
		
		if(where.size()!=0){
			buffer.append(tableName+" where");
			for (String key : params.keySet()) {
				Object value = params.get(key);
				String column = where.get(key);
				if(column!=null){

					String[] str = column.split(":");

					if(str.length==2){
						buffer.append(" "+str[0]+"=? "+str[1]);
					}else{
						buffer.append(" "+str[0]+"=? and");
					}
					
					param.add(value);
				}

			}
		}else{
			buffer.append(tableName);
		}
		
		
		String condition = view.getCondition();
		if(condition!=null&&where.size()==0){

		    if(condition.toLowerCase().contains("GROUP BY".toLowerCase())){
		        buffer.append(" "+condition+"and");
	            buffer.replace(buffer.length()-3, buffer.length(), "");
		    }else{
    			buffer.append(" where "+condition+"and");
    			buffer.replace(buffer.length()-3, buffer.length(), "");
		    }
		}else if(condition!=null&&where.size()!=0){
			buffer.append(" "+condition+"and");
		}
		
		if(where.size()!=0){
			buffer.replace(buffer.length()-3, buffer.length(), "");
		}
		

		return buffer.toString();
	}
	
	
	
	/**
	 * 获取映射表中的view
	 * @param view
	 * @return
	 */
	private View getView(String view){
		return this.viewMapper.get(view);
	}
	
	public String getMethod(String viewName){
		return viewMapper.get(viewName).getMethod();
	}
}
